package dao;

import pojo.Supplier;
import utils.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SupplierDAOImpl {

    private static final String TABLE_NAME = "Supplier";

    public static void add(Supplier supplier) throws SQLException {
        String sql = "INSERT INTO " + TABLE_NAME + " VALUES (?, ?, ?)";
        JDBCUtils.execute(sql, null, supplier.getSupplierName(), supplier.getSupplierAddress());
    }

    public static void delete(int supplierID) throws SQLException {
        String sql = "DELETE FROM " + TABLE_NAME + " WHERE supplier_id = ?";
        JDBCUtils.execute(sql, supplierID);
    }

    public static void update(Supplier supplier) throws SQLException {
        String sql = "UPDATE " + TABLE_NAME + " SET supplier_name = ?, supplier_address = ? WHERE supplier_id = ?";
        JDBCUtils.execute(sql, supplier.getSupplierName(), supplier.getSupplierAddress(), supplier.getSupplierID());
    }

    public static Supplier getById(int supplierID) throws SQLException {
        String sql = "SELECT * FROM " + TABLE_NAME + " WHERE supplier_id = ?";
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql, supplierID);
        if (resultSet.next()) {
            return createSupplierFromResultSet(resultSet);
        }
        return null;
    }

    public static List<Supplier> getAll() throws SQLException {
        List<Supplier> suppliers = new ArrayList<>();
        String sql = "SELECT * FROM " + TABLE_NAME;
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql);
        while (resultSet.next()) {
            Supplier supplier = createSupplierFromResultSet(resultSet);
            suppliers.add(supplier);
        }
        return suppliers;
    }

    public static int getCount() throws SQLException {
        String sql = "SELECT COUNT(*) AS count FROM " + TABLE_NAME;
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql);
        if (resultSet.next()) {
            return resultSet.getInt("count");
        }
        return 0;
    }

    public static List<Supplier> getByPage(int start, int pageSize) throws SQLException {
        List<Supplier> suppliers = new ArrayList<>();
        String sql = "SELECT * FROM " + TABLE_NAME + " LIMIT ?, ?";
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql, start, pageSize);
        while (resultSet.next()) {
            Supplier supplier = createSupplierFromResultSet(resultSet);
            suppliers.add(supplier);
        }
        return suppliers;
    }

    public static List<Supplier> findByKeyword(String keyword) throws SQLException {
        List<Supplier> suppliers = new ArrayList<>();
        String sql = "SELECT * FROM " + TABLE_NAME + " WHERE supplier_name LIKE ?";
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql, "%" + keyword + "%");
        while (resultSet.next()) {
            Supplier supplier = createSupplierFromResultSet(resultSet);
            suppliers.add(supplier);
        }
        return suppliers;
    }

    public static List<Supplier> findByPageAndKeyword(int start, int pageSize, String keyword) throws SQLException {
        List<Supplier> suppliers = new ArrayList<>();
        String sql = "SELECT * FROM " + TABLE_NAME + " WHERE supplier_name LIKE ? LIMIT ?, ?";
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql, "%" + keyword + "%", start, pageSize);
        while (resultSet.next()) {
            Supplier supplier = createSupplierFromResultSet(resultSet);
            suppliers.add(supplier);
        }
        return suppliers;
    }

    private static Supplier createSupplierFromResultSet(ResultSet resultSet) throws SQLException {
        int supplierID = resultSet.getInt("supplier_id");
        String supplierName = resultSet.getString("supplier_name");
        String supplierAddress = resultSet.getString("supplier_address");
        return new Supplier(supplierID, supplierName, supplierAddress);
    }
}
